<?php
import("@.ORG.Page"); 
class FirstAction extends Action
{
    //数据库模型对象
    protected $First;
    
 //   public  global $modDef=array("<input type='text' name='ctime' value='xx/yy/zz'/>","hello");
    //首页面
    public function Index()
    {
        $uid=Session::get('uid');
        $first=D('first')->where('uid='.$uid)->order('ctime desc')->findAll(6);
        $con['flag']='bborn';
        $flags=D('firstflag')->where($con)->findAll();
        
       $username=Session::get('uname');
       //取得历史添加的
        $list=$this->getPageList($uid);
       
       $firstList=$list['list'];
       foreach($firstList AS $key=>$val)
       {
         $firstList[$key]['time']=date('Y:m:d H:i:s',$val['ctime']); 
       }
       $this->assign("page",$list['page']);
       $this->assign("firstList",$firstList); 
       $this->assign("first",$first);
       $this->assign("flags",$flags);
       $this->assign('username',$username);
       $this->display('first');
        
    } 
    //根据id得到标签名称
    public function getflag(){
      $id=$_REQUEST['id'];
      $flag=D('firstflag')->where('id='.$id)->field('event')->find();        
      $this->ajaxReturn(null,$flag,1,'json'); 
    }
    
    //得到成长的标签内容  
    public function getotherflags(){
      $flag=$_REQUEST['flag'];
      $con['flag']=$flag;
      $flagList=D('firstflag')->where($con)->findAll();  
      if($flagList){
      $this->ajaxReturn($flagList,'',1,'json');
      }
        $this->ajaxReturn(null,'',0,'json');  
    }
    //删除某一条第一次
    public function delitem(){
      $id=$_REQUEST['id'];    
      $is=D('first')->where('id='.$id)->delete(); 
      if($is){
         $this->ajaxReturn(null,'',1,'json');         
      }
        $this->ajaxReturn(null,'',0,'json');       
    }   
     //根据id得到标签名称
    public function getFirstflagByEvent(){
      $event=$_REQUEST['recoverItem'];
      $con['event']=$event;
      $firstflag=D('firstflag')->where($con)->findAll();       
      $this->ajaxReturn($firstflag[0],'',1,'json'); 
    }
    
    //得到条目
    public function getItems(){
        //查询条件
        $condition['tag']=$_POST['tag'];
        $First=M('First');
        $List=$First->field('`id`,`name`')->where($condition)->findAll();
        $this->ajaxReturn($List,"",1,"JSON");    
    }
    //得到条目模板内容
    public function getContent(){
        //条目ID
        $condition['id']=$_POST['id'];
        if($_POST['id']){
            $First=M('First');
            $List=$First->field('`id`,`content`')->where($condition)->find();
            $this->ajaxReturn($List,"",1,"JSON");
        }      
    }
    //获取最新的10条first
    public function showbeforeFirst(){       
      $uid=Session::get('uid');
      $con['uid']=$uid;
      $first=D('first')->where($con)->order('ctime desc')->limit(0,10)->findAll();  
      if($first)
      {
         $this->ajaxReturn($first,"",1,"JSON");   
      }
      $this->ajaxReturn(null,"",0,"JSON"); 
    }
  //添加用户输入的里程碑内容
    public function addFirst(){
  //  $text=$_REQUEST['text'];
    $tag=$_REQUEST['tag'];
    $firsttime=$_REQUEST['firsttime'];
    $flag=$_REQUEST['flag'];
    $data['tag']=$tag;
    $data['ctime']=time();
    $data['name']=$flag;
    $data['content']=$firsttime;
    $data['uid']=Session::get('uid');
    $con1['uid']=$data['uid'];
    
    $taglist=D('first')->where($con1)->findAll();
    
    $bz=0;
    foreach($taglist AS $val){
        if($val['tag']==$tag){
            
         $eid=$val['id'];   
         $bz=1; 
         break;
        }
    }
    if($bz==1){
       $con2['id']=$eid; 
       
      $f_id=D('first')->where($con2)->save($data);  
      $this->ajaxReturn($eid,'添加成功！',1,'json');
    }
    else{
    $f_id=D('first')->add($data);
     $all['type']=1;
     $all['uid']=$data['uid'];
     $all['ctime']=$data['ctime'];
     $all['tid']=$f_id; 
    D('summary')->add($all);
    $this->ajaxReturn($f_id,'添加成功！',1,'json');  
    }
    $this->ajaxReturn(null,'添加成功！',0,'json');         
    }
    
    //得到用户需要编辑的里程碑内容
    public function editFirst(){
        //从页面得到的ID
        $id=2;
        $model=M();
        $reDef=array("id='ctime' value='xx/yy/zz'","id='item1' value=''","id='item2' value=''");
        $ans=$model->query('SELECT t1.content,t2.link,t2.fdate,t2.item1,t2.item2 FROM t_first t1,t_user_first t2 where t1.id=t2.fid and t2.id='.$id);      
        $ans[0]['content']=str_replace($reDef[0],"id='ctime' value='".date("Y-m-j",$ans[0]['fdate'])."'",$ans[0]['content']);
        //echo date("Y-m-j",$ans[0]['fdate']);
        
        if($ans[0]['item1']){
             $ans[0]['content']=str_replace($reDef[1],"id='item1' value='".$ans[0]['item1']."'",$ans[0]['content']);
        }
         if($ans[0]['item2']){
             $ans[0]['content']=str_replace($reDef[2],"id='item2' value='".$ans[0]['item2']."'",$ans[0]['content']);
        }
        
        echo $ans[0]['content'];
        return  $ans[0]['content'];
        
    }
    //响应用户编辑动作
    public function doEditFirst(){
        //得到页面传来的值
        if($_POST['item1']){
            $data['item1']=$_POST['item1'];
        } 
        if($_POST['item2']){
            $data['item2']=$_POST['item2'];
        }
        if($_POST['link']){
            $data['link']=$_POST['link'];
        }
        // $data['fid']=$_POST['fid'];
        //$data['uid']=$_SESSION['id'];
        $data['fdate']=$_POST['fdate'];
        //$data['cdate']=time();
        $User_First=M('userFirst');
        return $User_First->data($data)->save();
        
    }
    //删除里程碑
    public function doDeleteFirst(){ 
       if($_POST['id']){
        $condition['id']=$_POST['id'];
        $User_First=M('userFirst');
        $User_First->where($condition)->delete();
       }else{
            return 0;
       }       
    }
    
    //评论
    public function doComment(){
               
    }
 
   public function getPageList($uid){
    //处理where条件
    
  //  $limit = isset( $limit )?$limit:$this->config->limitpage;
  //  $result = $this->where( $map )->field( $field )->order( $order )->find() ;
 	    $count=D('first')->where('uid='.$uid)->count();   	    
		$p = new Page ( $count, 8);       
		$list=D('first')->where('uid='.$uid)->limit($p->firstRow.','.$p->listRows)->order('id desc')->field()->findAll();
		$p->setConfig('header','条记录');
		$p->setConfig('prev',"<");
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
        $page = $p->show();
	//	$this->assign ( "page", $page );
	//	$this->assign ( "list", $list );
        $result['page']=$page;
        $result['list']=$list;
        return $result;
   }  
  public function getAjaxFirst(){
    
     $uid=Session::get('uid');
     $list=$this->getPageList($uid);
 //  $favoriteList=$list['list'];
 //  foreach($favoriteList AS $key=>$val){
 //  $favoriteList[$key]['time']=date('Y-m-d H:i:s',$val['ctime']); 
 
 // } 
  // $this->assign("page",$list['page']);
  // $this->assign("favoriteList",$favoriteList); 
   
     $this->ajaxReturn($list,'',1,'json');
}   

    
}
?>